package com.herench.bunch.router;


import com.herench.bunch.commons.support.WsClientFactory;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component("webRouterService")
@javax.jws.WebService(endpointInterface = "com.herench.bunch.router.WsProxy", serviceName = "webRouterService", targetNamespace = "http://router.bunch.herench.com/")

public class WebRouterService implements WsProxy<String> {

    /**
     * 定义日志记录工具类，日志写入方式为异步
     */
    private static final Logger logger = LoggerFactory.getLogger(WebRouterService.class);

    /**
     * 定义路由存储map，需要线程安全
     */
    private static Map<String, RouterMapping> mappingMap = new ConcurrentHashMap<>();

    private static String defaultUrl;


    public Map<String, RouterMapping> getMappingMap() {
        return mappingMap;
    }

    public void setMappingMap(Map<String, RouterMapping> mappingMap) {
        this.mappingMap = mappingMap;
    }

    /**
     * 初始化方法
     */
    @PostConstruct
    protected void init() {
        /**
         * 读取resource下的router-config.xml 文件，读取方式为流式读取
         */
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("router-config.xml");
        try {
            /**
             * 转化成dom
             */
            Document document = new SAXReader().read(is);
            /**
             * 开始接续配置文件中的mappers节点
             */
            Element mapperConfigElement = document.getRootElement().element("mapper-config");
            defaultUrl = mapperConfigElement.elementText("default-url");

            Element mappers = mapperConfigElement.element("mappers");
            if (mappers != null) {
                for (Iterator<Element> iterator = mappers.elementIterator(); iterator.hasNext(); ) {
                    Element mapperEl = iterator.next();
                    WebRouterService.RouterMapping routerMapping = new WebRouterService.RouterMapping(mapperEl.elementText("method"), mapperEl.elementText("target-method"), mapperEl.elementText("target-url"));
                    mappingMap.put(routerMapping.getMethod(), routerMapping);
                }
            }
            /**
             * 结束mappers解析
             */
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }


    @Override
    public String send(String originalMessage) {
        String result = "";
        logger.info("originalMessage:" + originalMessage);
        try {
//            Document document = DocumentHelper.parseText(originalMessage);
//            Element clientInfo = document.getRootElement().element("TradeCode");
//            String method = clientInfo.getTextTrim();
            RouterMapping routerMapping = mappingMap.get("S001");
//            if (routerMapping == null) {
//                throw new RuntimeException("未找到对应的url,注意核对配置是否正确.");
//            } else {
                Client client = WsClientFactory.getInstance().getWsClient(routerMapping.getTargetUrl());
                Object[] res = client.invoke("QYIBusiness", originalMessage);
                logger.info("resMessage:" + res[0]);
                result = (String) res[0];
//            }
        } catch (Exception e) {
            logger.info("exception:" + e.getMessage());
            e.printStackTrace();
            result = e.getMessage() != null ? e.getMessage() : "空指针异常";
        }
        return result;
    }

    /**
     * 定义路由存储内部类
     */
    class RouterMapping {

        /**
         * 报文中的方法属性
         */
        private String method;
        /**
         * 目的调用方法名
         */
        private String targetMethod;
        /**
         * 目的调用wsdl
         */
        private String targetUrl;


        /**
         * 创建 三参数构造方法，以精简代码
         *
         * @param method
         * @param targetMethod
         * @param targetUrl
         */
        public RouterMapping(String method, String targetMethod, String targetUrl) {
            this.method = method;
            this.targetMethod = targetMethod;
            this.targetUrl = targetUrl;
            if (StringUtils.isEmpty(this.targetUrl)) {
                this.targetUrl = defaultUrl;
            }
        }

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public String getTargetMethod() {
            return targetMethod;
        }

        public void setTargetMethod(String targetMethod) {
            this.targetMethod = targetMethod;
        }

        public String getTargetUrl() {
            return targetUrl;
        }

        public void setTargetUrl(String targetUrl) {
            this.targetUrl = targetUrl;
        }
    }
}
